package com.regal.packet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

public class RecommandRoster extends IQ {

	private final List<RosterItem> comItems = new ArrayList<RosterItem>();
	private String userType;
	/**
	 * Element name of the packet extension.
	 */
	public static final String ELEMENT_NAME = "query";

	/**
	 * Namespace of the packet extension.
	 */
	public static final String NAMESPACE = "jabber:iq:recommand_roster";

	public void addRosterItem(RosterItem item) {
		synchronized (comItems) {
			comItems.add(item);
		}
	}

	public int getRosterItemCount() {
		synchronized (comItems) {
			return comItems.size();
		}
	}

	public Collection<RosterItem> getRosterItems() {
		synchronized (comItems) {
			return Collections.unmodifiableList(new ArrayList<RosterItem>(
					comItems));
		}
	}

	public String getChildElementXML() {
		StringBuffer buf = new StringBuffer();
		buf.append("<query xmlns=\"" + NAMESPACE + "\">");
		buf.append("</query>");
		return buf.toString();

	}

	/**
	 * A Company item, which consists of a companyid...
	 */
	public static class RosterItem {

		private String jid;

		private String name;
		
		private String email;
		
		private String occupation;
		
		private String companyname;

		
		public String getCompanyname() {
			return companyname;
		}


		public void setCompanyname(String companyname) {
			this.companyname = companyname;
		}


		public String getEmail() {
			return email;
		}


		public void setEmail(String email) {
			this.email = email;
		}


		public String getJid() {
			return jid;
		}


		public void setJid(String jid) {
			this.jid = jid;
		}


		public String getName() {
			return name;
		}


		public void setName(String name) {
			this.name = name;
		}


		public String getOccupation() {
			return occupation;
		}


		public void setOccupation(String occupation) {
			this.occupation = occupation;
		}


		/**
		 * Creates a new company item.
		 * 
		 * @param user
		 *            the companyid.
		 * @param name
		 *            the companyname.
		 */
		public RosterItem() {
			
		}

	}

	/**
	 * An IQProvider for CompanyListIQ packet.
	 * 
	 * @author Derek DeMoro
	 */
	public static class Provider implements IQProvider {

		public Provider() {
			super();
		}

		public IQ parseIQ(XmlPullParser parser) throws Exception {

			RecommandRoster recRoster = new RecommandRoster();
			RosterItem rosterItem = null;

			boolean done = false;
			while (!done) {
				int eventType = parser.next();
				if (eventType == XmlPullParser.START_TAG) {
					if (parser.getName().equals("item")) {
						rosterItem = new RosterItem();
						String jid = parser.getAttributeValue("", "jid");
						rosterItem.setJid(jid);
						String name = parser.getAttributeValue("", "name");
						rosterItem.setName(name);
						String email = parser.getAttributeValue("", "email");
						rosterItem.setEmail(email);
						String occupation = parser.getAttributeValue("", "occupation");
						rosterItem.setOccupation(occupation);
						String companyName = parser.getAttributeValue("", "companyname");
						rosterItem.setCompanyname(companyName);
					}
				}

				else if (eventType == XmlPullParser.END_TAG) {
					if (parser.getName().equals("item")) {
						recRoster.addRosterItem(rosterItem);
					}
					if (parser.getName().equals(ELEMENT_NAME)) {
						done = true;
					}
				}
			}
			return recRoster;
		}
	}



	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

}